<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品的ID" prop="commondityId">
      <el-input v-model="dataForm.commondityId" placeholder="商品的ID"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="生效状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="生效状态"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="" prop="purchaseInfoId">
      <el-input v-model="dataForm.purchaseInfoId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="orderDetailId">
      <el-input v-model="dataForm.orderDetailId" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          commondityId: '',
          unitPrice: '',
          remark: '',
          status: '',
          createUser: '',
          createTime: '',
          updateTime: '',
          updateUser: '',
          purchaseInfoId: '',
          orderDetailId: ''
        },
        dataRule: {
          commondityId: [
            { required: true, message: '商品的ID不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '生效状态不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          purchaseInfoId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          orderDetailId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commoditypricehistory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commondityId = data.commodityPriceHistory.commondityId
                this.dataForm.unitPrice = data.commodityPriceHistory.unitPrice
                this.dataForm.remark = data.commodityPriceHistory.remark
                this.dataForm.status = data.commodityPriceHistory.status
                this.dataForm.createUser = data.commodityPriceHistory.createUser
                this.dataForm.createTime = data.commodityPriceHistory.createTime
                this.dataForm.updateTime = data.commodityPriceHistory.updateTime
                this.dataForm.updateUser = data.commodityPriceHistory.updateUser
                this.dataForm.purchaseInfoId = data.commodityPriceHistory.purchaseInfoId
                this.dataForm.orderDetailId = data.commodityPriceHistory.orderDetailId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commoditypricehistory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'commondityId': this.dataForm.commondityId,
                'unitPrice': this.dataForm.unitPrice,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'updateUser': this.dataForm.updateUser,
                'purchaseInfoId': this.dataForm.purchaseInfoId,
                'orderDetailId': this.dataForm.orderDetailId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
